package com.atguigu.lease.web.app.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.atguigu.lease.web.app.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SmsServiceImpl implements SmsService {
  @Autowired private Client client;

  @Override
  /**
   * 发送验证码到指定手机号
   *
   * @param phone 手机号
   * @param verifyCode 验证码
   */
  public void sendCode(String phone, String verifyCode) {
    // 创建短信发送请求对象
    SendSmsRequest smsRequest = new SendSmsRequest();
    // 设置接收短信的手机号码
    smsRequest.setPhoneNumbers(phone);
    // 设置短信签名名称
    smsRequest.setSignName("阿里云短信测试");
    // 设置短信模板代码
    smsRequest.setTemplateCode("SMS_154950909");
    // 设置短信模板参数，将验证码插入模板参数中
    smsRequest.setTemplateParam("{\"code\":\"" + verifyCode + "\"}\n");

    try {
      // 执行发送短信操作
      client.sendSms(smsRequest);
    } catch (Exception e) {
      // 捕获异常并抛出运行时异常
      throw new RuntimeException(e);
    }
  }
}
